package com.huike.web.controller.business;

import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.service.ITbBusinessService;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.huike.common.core.controller.BaseController;

import java.util.List;

/**
 * 商机跟进记录Controller
 * @date 2021-04-28
 */
@RestController
@RequestMapping("/business/record")
public class TbBusinessTrackRecordController extends BaseController {
    @Autowired
    ITbBusinessTrackRecordService iTbBusinessTrackRecordService;
    @Autowired
    ITbBusinessService tbBusinessService;

    @GetMapping("/list")
    public AjaxResult recordList(TbBusinessTrackRecord tbBusinessTrackRecord){

        List<TbBusinessTrackRecord> tbBusinessTrackRecords =iTbBusinessTrackRecordService.selectTbBusinessTrackRecordList(tbBusinessTrackRecord);
        return AjaxResult.success(tbBusinessTrackRecords);
    }

    @PostMapping("")
    public AjaxResult record(@RequestBody BusinessTrackVo businessTrackVo){

        TbBusiness tbBusiness = new TbBusiness();
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo, tbBusinessTrackRecord);
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        iTbBusinessTrackRecordService.insertTbBusinessTrack(tbBusinessTrackRecord);

        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusiness.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbBusinessService.updateTbBusiness(tbBusiness);

        return AjaxResult.success();
    }
}
